package com.pic4493.adapter;

import java.util.ArrayList;

import com.pic4493.app.R;
import com.pic4493.entities.EReview;
import com.ppcodes.imageloader.core.DisplayImageOptions;
import com.ppcodes.imageloader.core.ImageLoader;

import android.R.integer;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AdpList_Reviews extends BaseAdapter
{
   private DisplayImageOptions imgConfig = new DisplayImageOptions.Builder().cacheInMemory().bitmapConfig(Bitmap.Config.RGB_565).build();
   private Context mContext;
   private ArrayList<EReview> mList;
   private LayoutInflater mInflater;
   private int[] mBackColors={R.drawable.bg_selector_listitem_white_cyan,R.drawable.bg_selector_listitem_brown_cyan};

   private void getData(ArrayList<EReview> list)
   {
	  try
	  {
		 if (list != null)
		 {
			for (int i = 0; i < list.size(); i++)
			{
			   mList.add(list.get(i));
			   if (list.get(i).getSonList() != null)
			   {
				  getData(list.get(i).getSonList());
			   }
			}
		 }
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "AdpList_Reviews_getData(list) " + e);
	  }
   }

   public String getParentName(EReview review)
   {
	  if (review.hasParent())
	  {
		 for (int i = 0; i < mList.size(); i++)
		 {
			if (mList.get(i).getReviewId().equals(review.getParentId()))
			{
			   return mList.get(i).getNickName();
			}
		 }
	  }
	  return null;
   }

   public AdpList_Reviews(Context context, ArrayList<EReview> list)
   {
	  mContext = context;
	  mInflater = LayoutInflater.from(context);
	  mList=new ArrayList<EReview>();
	  getData(list);
   }

   @Override
   public int getCount()
   {
	  return mList!=null?mList.size():0;
   }

   @Override
   public Object getItem(int position)
   {
	  return mList.get(position);
   }

   @Override
   public long getItemId(int position)
   {
	  return 0;
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent)
   {
	  try
	  {
		 ViewHolder holder;
		 if (convertView == null)
		 {
			convertView = mInflater.inflate(R.layout.uipictalk_item, null);
			holder = new ViewHolder();
			holder.layRepeat = (LinearLayout) convertView.findViewById(R.id.uipictalk_item_lay_repeat);
			holder.imgFace = (ImageView) convertView.findViewById(R.id.uipictalk_item_img_face);
			holder.txtNickName = (TextView) convertView.findViewById(R.id.uipictalk_item_txt_name);
			holder.txtToNickName = (TextView) convertView.findViewById(R.id.uipictalk_item_txt_name_repeat);
			holder.txtContent = (TextView) convertView.findViewById(R.id.uipictalk_item_txt_content);
			convertView.setTag(holder);
		 }
		 else
		 {
			holder = (ViewHolder) convertView.getTag();
		 }
         convertView.setBackgroundResource(mBackColors[position%2]);
         ImageLoader.getInstance().displayImage(mList.get(position).getAvatars(), holder.imgFace, imgConfig);
		 if (mList.get(position).hasParent())
		 {
			holder.layRepeat.setVisibility(View.VISIBLE);
			holder.txtToNickName.setText(getParentName(mList.get(position)));
		 }
		 else
		 {
			holder.layRepeat.setVisibility(View.GONE);
		 }

		 holder.txtNickName.setText(mList.get(position).getNickName());
		 holder.txtContent.setText(mList.get(position).getContent());

		 return convertView;
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "AdpList_Reviews_getView(position, convertView, parent) " + e);
	  }
	  return new View(mContext);
   }

   static class ViewHolder
   {
	  LinearLayout layRepeat;
	  ImageView imgFace;
	  TextView txtNickName;
	  TextView txtToNickName;
	  TextView txtContent;
   }
}
